package com.cgy.utils.jmx.system.disk;

import com.cgy.utils.jmx.IJvmInfoFactory;
import com.cgy.utils.jmx.jvm.constant.DiskInfo;
import com.cgy.utils.jmx.jvm.utils.SizeUtils;
import com.cgy.utils.jmx.system.hardware.Hardware;
import oshi.hardware.HWDiskStore;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author CHTK
 */
public class DiskSystemFactory extends IJvmInfoFactory {

    @Override
    public void status() {
        HWDiskStore[] hwDiskStores = Hardware.DISK;
        FileSystem fileSystem = Hardware.FILE_SYSTEM;

        for (HWDiskStore hwDiskStore : hwDiskStores) {
            DiskInfo diskInfo = new DiskInfo();
            String name = hwDiskStore.getName();

            long writes = hwDiskStore.getWriteBytes();
            long reads = hwDiskStore.getReadBytes();

            diskInfo.setName(name);
            diskInfo.setReads(reads);
            diskInfo.setReadsString(SizeUtils.getDataSize(reads));

            diskInfo.setWrites(writes);
            diskInfo.setWritesString(SizeUtils.getDataSize(writes));


            put(name, diskInfo, "IO信息");
        }

        OSFileStore[] fileStores = fileSystem.getFileStores();
        for (OSFileStore fileStore : fileStores) {
            DiskInfo diskInfo = new DiskInfo();
            long totalSpace = fileStore.getTotalSpace();
            long usableSpace = fileStore.getUsableSpace();

            long usedSpace = totalSpace - usableSpace;

            String dataSize = SizeUtils.getDataSize(totalSpace);
            String dataSize1 = SizeUtils.getDataSize(usableSpace);
            String dataSize2 = SizeUtils.getDataSize(usedSpace);

            diskInfo.setTotalSize(totalSpace);
            diskInfo.setTotalSizeString(dataSize);
            diskInfo.setFreeSize(usableSpace);
            diskInfo.setFreeSizeString(dataSize1);
            diskInfo.setUsedSize(usedSpace);
            diskInfo.setUsedSizeString(dataSize2);

            diskInfo.setDiskName(fileStore.getMount());
            diskInfo.setUuid(fileStore.getUUID());
            diskInfo.setName(fileStore.getName());
            diskInfo.setFsType(fileStore.getType());

            diskInfo.setUsedRate(dataSize2 + "/" + dataSize + "(" + SizeUtils.format(usedSpace, totalSpace) +")");
            diskInfo.setFreeRate(dataSize1 + "/" + dataSize + "(" + SizeUtils.format(usableSpace, totalSpace) +")");

            put(fileStore.getMount(), diskInfo, "文件信息");
        }

    }

    @Override
    public Map<String, String> fields() {
        return new HashMap<String, String>() {
            {
                put("os.name", "操作系统的名称");
                put("os.version", "操作系统的架构");
                put("os.arch", "操作系统的版本");
            }
        };
    }

}
